import { createApp } from "vue";
import App from "./App.vue";
import "@/assets/less/index.less";// 引入全局样式文件
import router from "./router";
import ElementPlus from "element-plus";
import "element-plus/dist/index.css";
import * as ElementPlusIconsVue from "@element-plus/icons-vue";
import { createPinia } from 'pinia';
import "@/api/mock.js";
import api from "@/api/api";
import { useAllDataStore } from "@/stores";
function isRoute(to) {
  let res = router.getRoutes();
  let resFil = res.filter((item) => item.path === to.path);
  return resFil.length > 0;
}
router.beforeEach((to, from) => {
  if (to.path !== '/login' && !store.state.token) {
    return { name: 'login' }
  }
  if (!isRoute(to)) {
    return { name: '404' };
  }
});
const pinia = createPinia(); // 创建 Pinia 实例
const app = createApp(App);

app.config.globalProperties.$api = api; // 将 api 挂载到全局属性上
app.use(ElementPlus);
app.use(pinia);

app.use(router);
const store = useAllDataStore(); // 注册 store
store.addMenu(router, "refresh"); // 注册路由


// 挂载应用
app.mount('#app');
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}
